<template>
  <div class="payment">
    <van-nav-bar
      title="确认订单"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      @click-right="$router.push('/search')"
    >
      <template #right> </template>
    </van-nav-bar>
    <div class="concent">
      <div class="more" @click="$router.push('/address')">
        <div class="address">
          <div class="info" v-if="isDefault">
            <p>
              <span>{{ isDefault.name }}</span
              ><span>{{ isDefault.tel }}</span>
            </p>
          </div>
          <div class="phone" v-if="isDefault">
            <p>{{ isDefault.address }}</p>
          </div>
          <div class="add" v-if="!isDefault">当前暂无地址前往添加</div>
        </div>
        <i class="iconfont icon-xiangyou1"></i>
      </div>
      <div class="strip"></div>
      <div class="items">
        <div class="item" v-for="(value, index) of item" :key="index">
          <div class="img">
            <img :src="value.image_url" />
          </div>
          <div class="info">
            <div class="name">
              <p>
                <span>{{ value.product_name }}</span>
              </p>
            </div>
          </div>
          <div class="price">
            <div class="item_price">
              <strong>{{ value.market_price }}</strong>
            </div>
          </div>
        </div>
      </div>
      <div class="strip"></div>
      <div class="attach">
        <dl>
          <dt class="left">
            <strong>运费</strong>
            <div class="right">
              <span>包邮</span>
            </div>
          </dt>
        </dl>
      </div>
      <div class="attach">
        <dl>
          <dt class="left">
            <strong>电子普通发票</strong>
            <div class="right">
              <span>个人</span>
            </div>
          </dt>
        </dl>
      </div>
      <div class="attach">
        <dl>
          <dt class="left">
            <strong>优惠券</strong>
            <div class="right">
              <span>无可用</span>
            </div>
          </dt>
        </dl>
      </div>
      <div class="strip"></div>
      <div class="concent_price">
        <p>
          <strong>商品价格：</strong><span>{{ total }}</span>
        </p>
        <p><strong>配送费用：</strong><span>0.00</span></p>
      </div>
      <div class="strip"></div>
    </div>
    <transition name="van-slide-up">
      <div class="footer" v-show="show">
        <div class="footer_price">
          <div class="price">
            <span>共{{ this.item.length }}件 合计: </span
            ><strong>{{ total }}</strong>
          </div>
          <div class="footer_btn" @click="pay">
            <span>去付款</span>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>
<script>
import Vue from "vue";
import { NavBar } from "vant";
import { mapActions, mapState } from "vuex";
import { Dialog } from "vant";
Vue.use(NavBar);
export default {
  name: "Payment",
  data() {
    return {
      show: false, //显示隐藏
    };
  },
  methods: {
    ...mapActions({ addOrders: "addOrders" }),
    //订单内容
    pay() {
      let time =
        new Date().toLocaleDateString().split("/").join("-") +
        " " +
        new Date().getHours() +
        ":" +
        new Date().getMinutes();
      let num = this.item.reduce((total, item) => {
        if (item.checked) {
          return (total += item.num);
        } else {
          return total;
        }
      }, 0);
      Dialog.confirm({
        title: "确认付款",
      })
        .then(() => {
          this.addOrders({
            orderList: this.item,
            type: "待发货",
            time,
            total: this.total,
            num,
            address: this.isDefault,
          });
          this.$router.go(-1);
        })
        .catch(() => {
          this.addOrders({
            orderList: this.item,
            type: "待付款",
            time,
            total: this.total,
            num,
            address: this.isDefault,
          });
          this.$router.go(-1);
        });
    },
  },
  mounted() {
    this.show = true;
  },
  computed: {
    ...mapState({
      list: "list",
      items: "items",
    }), //默认地址
    isDefault() {
      return this.list.filter((ele) => ele.select)[0];
    }, //列表
    item() {
      return this.items.filter((ele) => ele.checked);
    }, //价格
    total() {
      return this.item.reduce((total, item) => {
        if (item.checked) {
          return (total += item.price * item.num);
        } else {
          return total;
        }
      }, 0);
    },
  },
};
</script>

<style lang="less">
.payment {
  .van-nav-bar {
    position: sticky;
    top: 0;
    background-color: rgb(242, 242, 242);
    .van-nav-bar__title {
      color: #8a8a8a;
    }
    .van-nav-bar__left {
      .van-icon,
      .van-nav-bar__text {
        font-size: 16px;
        line-height: 46px;
        color: #8a8a8a;
      }
    }
  }
  .concent {
    .more {
      padding: 0.26rem 0.4rem;
      background: #fff url(https://s1.mi.com/m/images/m/bd1.png) 0 0 repeat-x;
      background-size: 0.52rem 0.08rem;
      padding-top: 0.4rem;
      padding-bottom: 0.3rem;
      position: relative;
      .icon-xiangyou1 {
        position: absolute;
        margin: auto 0;
        top: 0;
        bottom: 0;
        right: 0.3rem;
        height: 0.4rem;
        font-size: 0.4rem;
      }
      .address {
        .info {
          font-size: 0.3rem;
          font-weight: 700;
          margin-bottom: 0.2rem;
          span {
            margin-right: 0.1rem;
          }
        }
        .phone {
          line-height: 0.28rem;
          color: #757575;
          margin-right: 0.3rem;
          text-align: left;
          font-size: 0.24rem;
        }
        .add {
          font-size: 0.3rem;
          line-height: 0.8rem;
        }
      }
    }
    .strip {
      border-top: 1px solid #e0e0e0;
      border-bottom: 1px solid #e0e0e0;
      height: 0.2rem;
      background: #f5f5f5;
      overflow: hidden;
      clear: both;
    }
    .items {
      background: #fff;
      .item {
        display: flex;
        flex-direction: row;
        align-items: center;
        line-height: 0.3rem;
        margin: 0 0.4rem;
        padding: 0.14rem 0;
        font-size: 0.24rem;
        border-bottom: 1px solid #eee;
        &:last-child {
          border-bottom: none;
        }
        .img {
          margin-right: 0.2rem;
          width: 0.8rem;
          line-height: 0.3rem;
          height: 0.8rem;
          img {
            width: 0.8rem;
          }
        }
        .info {
          flex: 1;
          .name {
            line-height: 0.3rem;
            text-align: left;
          }
        }
      }
    }
    .attach {
      background: #fff;
      text-align: left;
      font-size: 0.24rem;
      .left {
        padding: 0.26rem 0.4rem;
        justify-content: space-between;
        align-items: center;
        display: flex;
        border-bottom: 1px solid #eee;
        .right {
          span {
            float: right;
            color: #bdbdbd;
          }
        }
      }
    }
    .concent_price {
      padding: 0.26rem 0.4rem;
      background: #fff;
      text-align: left;
      font-size: 0.24rem;
      p {
        line-height: 2em;
      }
    }
  }
  .footer {
    transform: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    border-top: 1px solid #e0e0e0;
    z-index: 10;
    .footer_price {
      display: flex;
      align-items: center;
      .price {
        font-size: 0.3rem;
        color: #ff4d14;
        width: 100%;
        text-align: center;
      }
    }
    .footer_btn {
      width: 100%;
      span {
        display: block;
        background-color: #ff5722;
        text-align: center;
        height: 1rem;
        line-height: 1rem;
        border: 1px solid #ff5722;
        font-size: 0.3rem;
        color: #fff;
      }
    }
  }
}
</style>